package com.mvc.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.mvc.dao.EntityDao;
import com.mvc.entity.Dict;
import com.pub.Page;

@Service
public class DictService {
	@Autowired
	private EntityDao entityDao;

	@Transactional(propagation=Propagation.NOT_SUPPORTED) 
	public List<Object> getDictList(String hql, Page page) {

		List<Object> list = entityDao.pageQuery(hql, page.getPageNo(),
				page.getPageSize());
		return list;
	}
	@Transactional(propagation=Propagation.NOT_SUPPORTED) 
	public int getCount(String hql, Page page) {
		int count = 0;
		count = entityDao.countQuery(hql);
		if ((count % page.getPageSize()) == 0) {
			count = count / page.getPageSize();
		} else {
			count = count / page.getPageSize() + 1;
		}
		return count;
	}
	@Transactional(propagation=Propagation.NOT_SUPPORTED) 
	public boolean IsExistDict(String Keyname) {
		boolean tag = false;
		if (Keyname == null || Keyname.equals("")) {
			tag = false;
		} else {
			String hql = "select count(*) from Dict a where a.Keyname='"
					+ Keyname.trim() + "'";
			int i = entityDao.countQuery(hql);
			if (i == 0) {
				tag = true;
			} else {
				tag = false;
			}
		}
		return tag;
	}
	@Transactional(propagation=Propagation.REQUIRED) 
	public void Save(Dict dict) {
		entityDao.save(dict);
	}
	@Transactional(propagation=Propagation.REQUIRED) 
	public void EditSave(Dict dict) {
		entityDao.saveOrUpdate(dict);
	}
	@Transactional(propagation=Propagation.REQUIRED) 
	public void DelDict(String Keyname) {
		String sql = "delete from Dict a where a.Keyname='" + Keyname + "'";
		entityDao.executeSql(sql);
	}
	@Transactional(propagation=Propagation.NOT_SUPPORTED) 
	public Dict getDict(String ID) {
		Dict dict = (Dict) entityDao.loadById(Dict.class, Integer.valueOf(ID));
		return dict;
	}
	@Transactional(propagation=Propagation.REQUIRED) 
	public void EditBySql(String sql) {

		entityDao.update(sql);
	}
	@Transactional(propagation=Propagation.NOT_SUPPORTED) 
	public List getAllByKeyname(String keyname) {
		String hql = "select a from Dict a where a.Keyname='" + keyname
				+ "' and a.Optname<>'请选择' order by a.id asc";
		List list = entityDao.query(hql);
		return list;
	}
	@Transactional(propagation=Propagation.REQUIRED) 
	public void Del(String ID) {
		Integer myid = Integer.valueOf(ID);
		entityDao.delById(Dict.class, myid);
	}

}
